package com.qiezikaka.kaka.adapter;

import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qiezikaka.kaka.R;
import com.qiezikaka.kaka.bean.FuZhuangInfo;

import java.util.List;

/**
 *
 */
public class FuZhuangDataListAdapter extends BaseQuickAdapter<FuZhuangInfo.DataBean.ListBean, BaseViewHolder> {

    public FuZhuangDataListAdapter(int layoutResId, List<FuZhuangInfo.DataBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FuZhuangInfo.DataBean.ListBean item) {
        helper.setText(R.id.tv_title, item.getTitle());
        helper.setText(R.id.tv_yipai, item.getNum_count() + "已拍");
        helper.setText(R.id.tv_zan, item.getNum_zan() + "已赞");

        ViewTreeObserver observer = helper.getView(R.id.rl_layout_view).getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                helper.getView(R.id.rl_layout_view).getViewTreeObserver().removeOnGlobalLayoutListener(this);
                ViewGroup.LayoutParams layoutParams = helper.getView(R.id.rl_layout_view).getLayoutParams();
                int width = helper.getView(R.id.rl_layout_view).getMeasuredWidth();
                layoutParams.height = width * 4 / 3;
                helper.getView(R.id.rl_layout_view).setLayoutParams(layoutParams);
            }
        });

        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontTransform()
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)//关键代码，加载原始大小
                .format(DecodeFormat.PREFER_RGB_565);//设置为这种格式去掉透明度通道，可以减少内存占有
//                .placeholder(R.drawable.img_error)
//                .error(R.drawable.img_error)

        Glide.with(mContext)
                .load(item.getThumb())
                .apply(requestOptions) // 使用 centerCrop() 防止拉伸
//                .placeholder(R.drawable.placeholder_image) // 占位符图片
//                .error(R.drawable.error_image) // 错误时显示的图片
                .into((RoundedImageView) helper.getView(R.id.riv_image));

    }

}


